#!/bin/sh /etc/rc.common
# Copyright (C) 2017 lean <coolsnowwolf@gmail.com>

START=99

start()
{
	rfc=4096
	cc=$(grep -c processor /proc/cpuinfo)
	rsfe=$(echo $cc*$rfc | bc)
	sysctl -w net.core.rps_sock_flow_entries=$rsfe >/dev/null
	for fileRps in $(ls /sys/class/net/eth*/queues/rx-*/rps_cpus)
	do
		echo $cc > $fileRps
	done
	
	for fileRfc in $(ls /sys/class/net/eth*/queues/rx-*/rps_flow_cnt)
	do
		echo $rfc > $fileRfc
	done
	
	uci set network.@globals[0].packet_steering=1
	uci commit network
	
	a=$(cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq)
	b=$(echo -n ' : ')
	c=$(cat /proc/cpuinfo | grep 'core id' | sort -u | wc -l)
	d=$(echo -n 'C')
	e=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
	f=$(echo -n 'T ')
	g=$(dmesg | grep 'DMI:' | awk -F ',' '{print $1 }' | awk -F ':' '{print $2 }')
	
	if [ -d /sys/devices/cpu_atom/ ]; then
    pcore=$(cat /sys/devices/cpu_core/cpus | awk -F- '{print $2}') 
    pc=$(echo "($pcore+1)/2" | bc) 
    ecore1=$(cat /sys/devices/cpu_atom/cpus | awk -F- '{print $1}') 
    ecore2=$(cat /sys/devices/cpu_atom/cpus | awk -F- '{print $2}')
    ec=$(echo "$ecore2-$ecore1+1" | bc) 
    hydrid='('${pc}'P+'${ec}'E)'
  fi
	
	h=${g}' - '${a}${b}${c}${d}${e}${f}${hydrid}

	mkdir -p /tmp/sysinfo
	echo $h > /tmp/sysinfo/model
	
	all_pcis=`lspci | grep -i 'eth' | grep -i 'x550' | cut -d ' ' -f 1`
	all_ifs=`cat /proc/net/dev | grep -i 'eth' | cut -d :  -f 1 | sed 's/^[ \t]*//g'`

	for ifname in ${all_ifs}
	do
		if_pci=`ethtool -i ${ifname} | grep -i 'bus-info' | cut -d : -f 3-`
		if [[ "$all_pcis" =~ "$if_pci" ]]
		then
			ethtool -s ${ifname} advertise 0x1800000001028
		fi
	done

	a=$(ip address | grep ^[0-9] | awk -F: '{print $2}' | sed "s/ //g" | grep '^[e]' | grep -v "@" | grep -v "\.")
	b=$(echo "$a" | wc -l)
	for i in $(seq 1 $b)
	do
		c=$(echo "$a" | sed -n ${i}p)
		ethtool -K $c rx-checksum on >/dev/null 2>&1
		ethtool -K $c tx-checksum-ip-generic on >/dev/null 2>&1 || (
		ethtool -K $c tx-checksum-ipv4 on >/dev/null 2>&1
		ethtool -K $c tx-checksum-ipv6 on >/dev/null 2>&1)
		ethtool -K $c tx-scatter-gather on >/dev/null 2>&1
		ethtool -K $c gso on >/dev/null 2>&1
		ethtool -K $c tso on >/dev/null 2>&1
		ethtool -K $c ufo on >/dev/null 2>&1
	done

	[ -f /etc/index.htm ] && mv /etc/index.htm /usr/lib/lua/luci/view/admin_status/index.htm
}